package com.hxci.zhangyong.controller;

import com.hxci.zhangyong.dao.TeacherDao;
import com.hxci.zhangyong.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("tea")
public class TeacherController {
    @Autowired
    TeacherDao dao;

    @RequestMapping("query")
    public String query(Model model){
        List<Teacher> list = dao.query();
        model.addAttribute("list",list);
        return "list";
    }

    @RequestMapping("add")
    public String add(){

        return "add";
    }

    @RequestMapping("save")
    public String save(Teacher teacher){
        if (teacher.getId() == null || teacher.getId().equals("") ){
            int a = dao.save(teacher);
        }else{
            int a = dao.update(teacher);
        }

        return "redirect: query";
    }

    @RequestMapping("del")
    public String del(Integer id){
        int a = dao.del(id);
        return "redirect: query";
    }

    @RequestMapping("index")
    public String index(){

        return "index";
    }

    @RequestMapping("edit")
    public String edit(Integer id,Model model){
        Teacher te = dao.edit(id);
        model.addAttribute("te",te);
        return "add";
    }
}
